/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.jabber;

import org.json.JSONObject;
import org.webos.java.base.Dialog;
import org.webos.java.base.DialogType;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.RemoteMessage;
import org.webos.java.jabber.tree.ConnectionItem;
import org.webos.java.jabber.tree.GroupItem;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */

@DialogType(jsClass="jabber.AddEntry")
public class AddEntryDialog extends Dialog{

    private JabberApp jabberApp = null;
    private GroupItem groupItem = null;
    private ConnectionItem connectionItem = null;

    public AddEntryDialog(JabberApp app, GroupItem aGroupItem, ConnectionItem aConnectionItem) {
        super(app);
        this.jabberApp = app;
        this.groupItem = aGroupItem;
        this.connectionItem = aConnectionItem;
        getApplication().getSession().addDialog(this);
    }

    @RemoteMessage
    public JSONObject addEntry(JSONObject aData){
        try {
            connectionItem.getRoster().createEntry(aData.optString("user"), aData.optString("name", null),
                    groupItem.getObject()==null?null:new String[]{groupItem.getObject().getName()});
            closeDialog(null);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
            return NetworkManager.getErrorObject(814, "Can't add new entry");
        }
        return null;
    }

}
